import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.edu.scau.cmi.huangminbin.dao.StudentDAO;
import cn.edu.scau.cmi.huangminbin.dao.TeacherDAO;
import cn.edu.scau.cmi.huangminbin.domain.Student;
import cn.edu.scau.cmi.huangminbin.domain.Teacher;
import cn.edu.scau.cmi.huangminbin.util.ScauCmiHibernateSessionFactoryUtil;

public class client {
	public static void main(String[] args) {
		/**
		 * 测试成功
		 */
//		Teacher teacher=new Teacher();		
//		teacher.setName("梁早清老师");
//		teacher.setId(2019L);		
//		newTeacher(teacher);
		
//		Student teacher=new Student();
//		teacher.setName("黄敏滨");
//	    teacher.setId(20190406L);		
//		newStudent(teacher);

		listTeachers();
		listStudents();
	}

		public static void newTeacher(Teacher teacher) {
			TeacherDAO t=new TeacherDAO();
			Transaction transaction = t.getSession().beginTransaction();
//	 		(1) 直接使用Hibernate的CRUD功能		
			t.save(teacher);
			transaction.commit();
		}

		public static void newStudent(Student student) {
			StudentDAO t=new StudentDAO();
			Transaction transaction = t.getSession().beginTransaction();
//	 		(1) 直接使用Hibernate的CRUD功能		
			t.save(student);
			transaction.commit();
		}

	private static void listTeachers() {
		TeacherDAO TeacherDAO=new TeacherDAO();
		List Teachers = TeacherDAO.findAll();
		Iterator<?> TeacherIterator = Teachers.iterator();
		while(TeacherIterator.hasNext()){
		Teacher Teacher = (Teacher) TeacherIterator.next();
		System.out.println("调用DAO后的老师名是："+Teacher.getName()+"，调用DAO后的老师ID号是："+Teacher.getId());
		}
	}
		private static void listStudents() {
			StudentDAO sDAO=new StudentDAO();
			List students = sDAO.findAll();
			Iterator<?> sIterator = students.iterator();
			while(sIterator.hasNext()){
				Student s = (Student) sIterator.next();
			System.out.println("调用DAO后的学生名是："+s.getName()+"，调用DAO后的学生ID号是："+s.getId());
			}
	}
}
